<template>
  <div class='articlelist'>
    <!-- 控制下拉刷新 -->
<van-pull-refresh success-duration='1500' :success-text="tiptext" v-model="isLoading" @refresh="onRefresh">
     <!--   v-model="loading"控制是否加载更多 -->
    <!-- :finished="finished" 数据全部加载成功 -->
<van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  :error.sync="error"
  error-text="请求失败，点击重新加载"
>
  <!-- <van-cell v-for="item in list" :key="item.art_id" :title="item.title" /> -->
  <article-item :row='item'  v-for="item in list" :key="item.art_id+''" :title="item.title"> </article-item>
</van-list>
</van-pull-refresh>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article.js'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  components: { ArticleItem },
  name: 'article1',
  props: {
    channelId: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      error: false,
      isLoading: false,
      tiptext: ''
    }
  },

  created () {

  },

  methods: {
    async  onLoad () {
      // 进入页面占满一屏 没有占满一屏 若加载状态为true则不会暂占满一屏
      // 下拉加载
      // 触底更新
      try {
        const res = await getArticleList({
          channel_id: this.channelId,
          timestamp: this.timestamp,
          with_top: 1
        })
        // 测试错误
        if (Math.random() > 0.5) {
          console.lg(12)
        }
        // 保存数据 添加进去不然会后一页覆盖前一页
        this.list.push(...res.results)
        // 让时间戳为数据返回的时间戳
        this.timestamp = res.pre_timestamp
        // 让当前这一次加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (!res.results.length) {
          this.finished = true
        }
      } catch (err) {
        // 随机出错
        this.error = true
        // 让加载状态结束
        this.loading = false
      }
    },

    // 下拉刷新
    async onRefresh () {
      try {
        const res = await getArticleList({
          channel_id: this.channelId,
          timestamp: Date.now(),
          with_top: 1
        })
        // 测试错误
        if (Math.random() > 0.5) {
          console.lg(12)
        }
        console.log(res)
        this.list.unshift(...res.results)
        this.isLoading = false
        this.tiptext = '刷新成功'
      } catch (err) {
        this.isLoading = false
        this.tiptext = '刷新失败,请重试！'
      }
    }
  }
}
</script>

<style scoped lang='less'>
.articlelist{
  height: 80vh;
  overflow-y:scroll;
}
</style>
